package com.li.controller;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 8/18/2022 10:07 AM
 */
@RestController
public class FileController {

    @RequestMapping("/test1")
    public void test1(HttpServletResponse res) throws IOException {
        Workbook workbook = new HSSFWorkbook();
        res.setCharacterEncoding("UTF-8");
        res.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        res.setContentType("application/vnd.ms-excel;charset=utf-8");
        res.setHeader("Content-Disposition", "attachment;filename='测试'");
        OutputStream outputStream = res.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();
    }

    @RequestMapping("/test2")
    public void test2(HttpServletResponse res) throws IOException {
        Workbook workbook = new HSSFWorkbook();
        res.setCharacterEncoding("UTF-8");
        res.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        res.setContentType("application/octet-stream;charset=utf-8");
        res.setHeader("Content-Disposition", "attachment;filename='测试'");
        OutputStream outputStream = res.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();
    }

    @RequestMapping("/test3")
    public void test3(HttpServletResponse res) throws IOException {
        Workbook workbook = new HSSFWorkbook();
        res.setCharacterEncoding("UTF-8");
        res.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        res.setContentType("application/octet-stream;application/vnd.ms-excel;charset=utf-8");
        res.setHeader("Content-Disposition", "attachment;filename='测试'");
        OutputStream outputStream = res.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();
    }
}
